#include <QDebug>
#include <QCompleter>
#include <QStringListModel>
#include <QStringBuilder>
#include <QDir>
#include <QPushButton>
#include "CreateClass.h"
#include "ui_CreateClass.h"
#include "Include/Project.h"
#include "Include/IRopeProject.h"
#include "Template.h"

CreateClass::CreateClass(const QString & path, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::CreateClass),
    _path(path)
{
    ui->setupUi(this);

    ui->edtFileName->setBasePath(path);
    ui->edtFileName->setExtension("py");

    QCompleter *parentCompleter =  new QCompleter(this);
    parentCompleter->setCaseSensitivity(Qt::CaseInsensitive);
    ui->edtParent->setCompleter(parentCompleter);

    Plugins::Project::currentInstance()->rope()->classNames("");

    _relPath = QDir(Plugins::Project::currentInstance()->path()).relativeFilePath(_path);
    ui->pathName->setText(_relPath);
    ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);

    connect(Plugins::Project::currentInstance()->rope(), SIGNAL(classListComplete(QStringList)), SLOT(onClassList(QStringList)));
    connect(ui->edtClassName, SIGNAL(textChanged(QString)), SLOT(onClassChanged(QString)));
    connect(ui->edtParent->completer(), SIGNAL(highlighted(QString)), SLOT(onParentHighlighted(QString)));
    connect(ui->edtParent, SIGNAL(editingFinished()), SLOT(onParentFinished()));
    connect(ui->edtFileName, SIGNAL(textChanged(QString)), SLOT(onFileNameChanged(QString)));
    connect(ui->buttonBox, SIGNAL(accepted()), SLOT(onAccept()));
}

CreateClass::~CreateClass()
{
    delete ui;
}

void CreateClass::onClassList(const QStringList & list)
{
    QStringList val;
    foreach(QString str, list){
        val = str.split(':');
        _classes.insert(val[0], val[1]);
    }
    ui->edtParent->completer()->setModel(new QStringListModel(_classes.keys(), this));
}

void CreateClass::onParentHighlighted(const QString & cls)
{
    if (_classes.contains(cls))
        ui->edtImport->setText(_classes[cls]);
}

void CreateClass::onParentFinished()
{
    QString txt = ui->edtParent->text();
    if (_classes.contains(txt))
        ui->edtImport->setText(_classes[txt]);
}

void CreateClass::onClassChanged(const QString & text)
{
    ui->edtFileName->setText(text);// % ".py");
    ui->edtFileName->normFileName();
    checkOk();
}

void CreateClass::onFileNameChanged(const QString & text)
{
    if (text == ""){
        ui->pathName->setText("");
        return;
    }
    checkOk();
}

void CreateClass::checkOk()
{
    bool result = ui->edtClassName->text() != "" && !ui->edtFileName->exists();
    ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(result);
}

void CreateClass::onAccept()
{
    Template tmpl("class");
    QMap<QString, QString> items;
    items["class-name"] = ui->edtClassName->text();
    items["class-parent"] = ui->edtParent->text() != "" ? ui->edtParent->text() : "object";
    items["args"] = (ui->edtParent->text().startsWith("Qt") || ui->edtParent->text().startsWith("Q")) ? ", parent = None" : "";
    items["set-args"] = (ui->edtParent->text().startsWith("Qt") || ui->edtParent->text().startsWith("Q")) ? "parent" : "";
    items["import"] = ui->edtImport->text();
    items["if-init"] = items["class-parent"] != "object" ? "true" : "false";
    QFile file(ui->edtFileName->fullFileName());
    if (file.open(QIODevice::WriteOnly)){
        file.write(tmpl.Render(items).toUtf8());
        file.close();
    }
    accept();
}

QString CreateClass::openAfter()
{
    return ui->edtFileName->fullFileName();
}
